package com.example.group.network.express_plus_app.Dao;

import android.content.Context;

import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;

import com.example.group.network.express_plus_app.Dao.UserDao.UserDao;
import com.example.group.network.express_plus_app.data.model.LoggedInUser;

/**
 * @author 夏宁
 * @email 1941512303@qq.com
 * @date 2021/3/24 0:16
 */
@Database(entities = {LoggedInUser.class}, version = 1, exportSchema = false)
public abstract class AppDatabase extends RoomDatabase {
    private static AppDatabase sInstance;

    private static final String DB_NAME = "AppDatabase.db";

    // 构造函数必须是public，否则报错
    public AppDatabase() {
    }

    // 单实例模式，节省资源
    public static AppDatabase getsInstance(Context context) {
        if (sInstance == null) {
            synchronized (AppDatabase.class) {
                if (sInstance == null) {

                    sInstance = create(context);                }
            }
        }
        return sInstance;
    }
    private static AppDatabase create(final Context context) {
        return Room.databaseBuilder(
                context,
                AppDatabase.class,
                DB_NAME)
                .allowMainThreadQueries()
                .build();
    }



    public abstract UserDao userDao();
}
